<!--登录记录-->
<template>
  <div class="app-container">
    <!--搜索条件-->
    <div class="filter-container">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="时间">
          <el-date-picker v-model="dateTime" type="datetimerange" value-format="timestamp" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="handleFilter">搜索</el-button>
          <el-button icon="el-icon-refresh" plain @click="handleReset">重置</el-button>
          <el-button plain icon="el-icon-brush" @click="handleClearLog">全部清空</el-button>

        </el-form-item>
      </el-form>
    </div>
    <c-list
      :data-source="loginLogs"
      empty="暂无登录日志"
      layout="horizontal"
      :split="false"
      :gutter="15"
      item-layout="vertical"
      item-width="350px"
      :item-between="true"
      :item-border="true"
      class="common-list"
      padding="0 10px">
      <template #item="{item}">
        <c-item :title="item.remark">
          <template #titleTag>
            <dot v-if="item.isSuccess===1" color="#4a5dff" />
            <dot v-if="item.isSuccess===0" color="#FC5E65" />
          </template>
          <template #desc>
            <div class="desc-item">
              <span class="label">操作环境：</span>
              {{ item.loginType }}.
              {{ item.browser }}<span v-if="item.browser" class="color-primary"> ({{ item.browserVersion }})</span>.
              {{ item.os }}
            </div>
            <div class="desc-item">
              <span class="label">登录地：</span>
              <span v-if="item.loginAddress"> {{ item.loginAddress }}</span>
              <span v-if="item.loginIp" class="color-primary"> ({{ item.loginIp }})</span>
            </div>
          </template>
          <template>
            <div class="tr color-default">
              {{ item.gmtCreated | formatDate }}
            </div>
          </template>
        </c-item>
      </template>
      <template v-show="total>0" #pagination>
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
      </template>
    </c-list>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginLogs: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 21,
        sort: this.common.SORT_DEFAULT
      },
      dateTime: [],

      multipleSelectionRecords: [],
      dialogVisible: false
    }
  },
  watch: {

  },
  created() {
    this.fetchData()
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    async fetchData() {
      if (this.dateTime.length > 0) {
        this.listQuery.startTime = this.dateTime[0]
        this.listQuery.endTime = this.dateTime[1]
      }
      const response = await this.$store.dispatch('currentUserLoginHistoryPage', this.listQuery)
      this.loginLogs = response.records
      this.total = response.total
    },
    /**
     * 全选/反选
     */
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },

    // 固定写法
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /**
     * 重置
     */
    handleReset() {
      this.dateTime = []
      this.listQuery.startTime = ''
      this.listQuery.endTime = ''
      this.listQuery.sort = this.common.SORT_DEFAULT
      this.handleFilter()
    },
    handleClearLog() {
      this.$confirm('此操作将清空您登录数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('currentUserLoginHistorClear').then(response => {
          this.handleFilter()
          this.$modal.msgSuccess('清空完成')
        }).catch(() => {})
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped >
</style>
